<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 公告控制器
 */

class Announce extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('announce_model');
	}

	public function index()
	{
		//get获取查询参数
		$page = intval($this->input->get('page', true)); //页数
		$action = $this->input->get('action', true);
		//$where['subject'] = $this->input->get('subject', TRUE);

		//查询组合
		$time = strtotime(date('Y-m-d'));
		if ($action == 'expired')
		{
			//过期
			$where['end_date <'] = $time;
		}
		else
		{
			$where['end_date >='] = $time;
		}

		//分页
		$count = $this->announce_model->count_all_list($where); //查询总数
		$config['base_url'] = site_url('admin/announce/index?'); //基本url
		$config['total_rows'] = $count; //总数据条数
		$config['per_page'] = 10; //每页显示多少条数据
		$this->load->library('pagination', $config);

		//根据条件查询需要显示的数据
		$announces = array();
		if ($count)
		{
			$limit = $config['per_page']; //$this->pagination->per_page;
			$offset = $this->pagination->get_offset($page);
			$announces = $this->announce_model->get_all_list($where, $limit, $offset, 'vieworder ASC');
		}
		$data['announces'] = $announces;
		$data['get'] = $where;
		$data['action'] = $action;
		$this->load->helper('form');
		$this->load->view('admin/announce_index', $data);
	}

	/**
	 * 排序操作
	 */
	public function do_vieworder_ajax()
	{
		$vieworders = $this->input->post('vieworders', TRUE);
		$vieworders === FALSE && show_admin_msg('没有内容要排序');

		$update = array();
		foreach ($vieworders as $k => $v)
		{
			//$vieworders[$k] = (int)$v;
			$update[] = array('announceid' => (int)$k, 'vieworder' => (int)$v);
		}

		$this->announce_model->update_batch($update);

		show_admin_msg('操作成功', 'success');
	}

	/**
	 * 添加公告页面
	 */
	public function add()
	{
		$this->load->view('admin/announce_add');
	}

	/**
	 * 添加公告操作
	 */
	public function do_add()
	{
		//input
		$vieworder = intval($this->input->post('vieworder', TRUE));
		$start_date = trim($this->input->post('start_date', TRUE));
		$end_date = trim($this->input->post('end_date', TRUE));
		$type = $this->input->post('type', TRUE);
		$subject = trim($this->input->post('subject', TRUE));
		$content = $this->input->post('content', TRUE);
		$url = trim($this->input->post('url', TRUE));

		//验证
		if ($start_date == FALSE || $end_date == FALSE)
		{
			show_admin_msg('亲，【有效时间】别忘记填写哦');
		}
		if (strtotime($start_date) > strtotime($end_date))
		{
			show_admin_msg('亲，【开始时间】不能大于【结束时间】哦');
		}
		if (empty($subject))
		{
			show_admin_msg('亲，【公告标题】别忘记填写哦');
		}
		if ($type == 'url' && empty($url))
		{
			show_admin_msg('亲，【公告链接】别忘记填写哦');
		}
		elseif ($type == 'text' && empty($content))
		{
			show_admin_msg('亲，【公告内容】别忘记填写哦');
		}

		//入库数据
		$insert['vieworder'] = $vieworder;
		$insert['start_date'] = strtotime($start_date);
		$insert['end_date'] = strtotime($end_date);
		$insert['type'] = $type;
		$insert['subject'] = $subject;
		$insert['content'] = $content;
		$insert['url'] = $url;

		$insert['uid'] = '1';
		$insert['username'] = 'admin';
		$insert['created'] = time();

		$this->announce_model->insert($insert);

		//redirect('admin/announce/index');

		show_admin_msg('发布成功', 'success', site_url('admin/announce/index'));
	}

    /**
	 * 删除操作(批量)
	 */
	public function do_dels_ajax()
	{
		$announceids = $this->input->post('announceids', TRUE);
		$announceids == FALSE && show_admin_msg('选择后才允许操作');

		foreach ($announceids as $announceid)
		{
			$this->announce_model->delete($announceid);
		}

		show_admin_msg('操作成功', 'success');
	}
}

/* End of file announce.php */
/* Location: ./application/controllers/admin/announce.php */
